/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.TX;

import entity.Outlet;
import entity.OutletSeatArea;
import entity.Timeslot;
import Helper.Helper_Area;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Lu Xiaodi
 */
@Stateless
public class AreaSB implements AreaSBRemote {

    @PersistenceContext()
    EntityManager em;

    public List<OutletSeatArea> getAllAreas(Long outletId) {
        Outlet outlet = em.find(Outlet.class, outletId);
        if (outlet == null) {
            return null;
        }
        List returnList = new ArrayList<OutletSeatArea>();
        for (OutletSeatArea os : outlet.getSeatingAreas()) {
            returnList.add(os);
        }
        return returnList;
    }

    public OutletSeatArea getAreaByAreaName(List<OutletSeatArea> areaList, String areaName) {
        OutletSeatArea area = new OutletSeatArea();

        for (int i = 0; i < areaList.size(); i++) {
            area = areaList.get(i);
            if (area.getAreaName().equalsIgnoreCase(areaName)) {
                return area;
            }

        }
        return null;
    }

    public List<Helper_Area> getAllHelperAreas(Long outletId) {
        List<Helper_Area> areaList = new ArrayList<Helper_Area>();
        Outlet outlet = em.find(Outlet.class, outletId);
        if (outlet == null) {
            return null;
        }
        List<OutletSeatArea> areas = outlet.getSeatingAreas();
        for (OutletSeatArea area : areas) {
            List<Integer> numDiners = area.getNumDiners();
            String numDinerStr = "";
            for (int i = 0; i < numDiners.size(); i++) {
                numDinerStr += numDiners.get(i);
                if (i < numDiners.size() - 1) {
                    numDinerStr += ", ";
                }
            }
            Helper_Area helper_Area = new Helper_Area(area.getId(), outlet.getId(),
                    outlet.getOutletName(), area.getAreaName(), area.getPathName(), numDinerStr);
            areaList.add(helper_Area);
        }
        return areaList;
    }

    public void saveArea(Helper_Area helper) {
        OutletSeatArea modifyArea = em.find(OutletSeatArea.class, helper.getId());
        //modify name
        modifyArea.setAreaName(helper.getAreaName());
        
        //modify numDiners
        String[] numDinerStrSplit = helper.getNumDinersStr().split(",");
        List<Integer> numDiners = new ArrayList<Integer>();
        try{
            for (int i=0;i<numDinerStrSplit.length;i++){
                numDiners.add(Integer.parseInt(numDinerStrSplit[i]));
            }
        } catch (Exception e){
            return;
        }
        modifyArea.setNumDiners(numDiners);
        em.persist(modifyArea);
    }

    public String addArea(Long outletId, String areaName, String pathName) {
        Outlet outlet = em.find(Outlet.class, outletId);
        if (outlet == null) {
            return "outlet not found!";
        }
        for (OutletSeatArea a : outlet.getSeatingAreas()) {
            if (a.getAreaName().equalsIgnoreCase(areaName)) {
                return "cannot have duplicate area names!";
            }
        }
        OutletSeatArea newArea = new OutletSeatArea();
        newArea.create(outlet.getId(), areaName, pathName);

        outlet.addArea(newArea);
        em.persist(outlet);
        return "area added successfully!";
    }

    public String addArea(Long outletId, String areaName, String pathName, List<Integer> numDinerAllowed) {
        Outlet outlet = em.find(Outlet.class, outletId);
        if (outlet == null) {
            return "outlet not found!";
        }
        for (OutletSeatArea a : outlet.getSeatingAreas()) {
            if (a.getAreaName().equalsIgnoreCase(areaName)) {
                return "cannot have duplicate area names!";
            }
        }
        OutletSeatArea newArea = new OutletSeatArea();
        newArea.create(outlet.getId(), areaName, pathName);
        newArea.setNumDiners(numDinerAllowed);

        outlet.addArea(newArea);
        em.persist(outlet);
        return "area added successfully!";
    }

    public String removeArea(Long id, Long areaId) {
        Outlet outlet = em.find(Outlet.class, id);
        if (outlet == null) {
            return "outlet not found!";
        }
        final OutletSeatArea theArea = em.find(OutletSeatArea.class, areaId);
        if (theArea == null) {
            return "area not found!";
        }

        outlet.removeArea(theArea);
        em.remove(theArea);
        em.persist(outlet);
        return "area successfully removed";
    }

    public List<Integer> retrieveTime(List<Timeslot> time) {
        List<Integer> timeList = new ArrayList<Integer>();

        for (int i = 0; i < time.size(); i++) {
            Timeslot ts = time.get(i);
            timeList.add(ts.getTsTime());
        }

        return timeList;
    }

    public List<Integer> getNumDiners(Long areaId) {
        OutletSeatArea a = em.find(OutletSeatArea.class, areaId);
        if (a == null) {
            return new ArrayList<Integer>();
        }
        return a.getNumDiners();
    }

    public void addNumDinerChoice(Long areaId, Integer num) {
        OutletSeatArea a = em.find(OutletSeatArea.class, areaId);
        if (a == null) {
            return;
        }
        for (Integer i : a.getNumDiners()) {
            if (i.intValue() == num.intValue()) {
                return;
            }
        }
        a.addNumDiner(num);
        em.persist(a);
    }

    public void deleteNumDinerChoice(Long areaId, Integer num) {
        OutletSeatArea a = em.find(OutletSeatArea.class, areaId);
        if (a == null) {
            return;
        }
        a.getNumDiners().remove(num);
        em.persist(a);
    }
}
